/*
 * Created on Jan 13, 2009
 */
package ak.salmon.markup;

import java.util.List;

import ak.salmon.model.NodeContent;

/**
 * Wraps around the MarkupWriter to provide high level svg drawing functions.
 * 
 * @author albert_kwong
 */
public class SvgDrawingPalette implements DrawingPalette {

    private MarkupWriter writer;

    private int fontSize = 12;
    private int textHeight = 15;
    private String lineStyle = "stroke-width:2;stroke:black";
    private String boxStyle = "stroke-width:2;stroke:black;fill:white;";

    public SvgDrawingPalette() {
        writer = new MarkupWriter();
    }

    public void setFontSize(int f) {
        this.fontSize = f;
    }

    public void setTextHeight(int h) {
        this.textHeight = h;
    }

    public void setLineStyle(String css) {
        this.lineStyle = css;
    }

    public void setBoxStyle(String css) {
        this.boxStyle = css;
    }

    public void begin(int width, int height) {
        writer.element("svg", "version", "1.1");
        writer.attribute("xmlns", "http://www.w3.org/2000/svg");
        writer.attribute("width", width + "px");
        writer.attribute("height", height + "px");
        writer.attribute("viewBox", "0 0 " + width + " " + height);
        writer.element("style", "type", "text/css");
        writer.cdata("\n");
        writer.cdata(".boxStyle {" + boxStyle + "}\n");
        writer.cdata(".lineStyle {" + lineStyle + "}\n");
        writer.end();
    }

    public void end() {
        writer.end();
    }

    public String toString() {
        return writer.toString();
    }

    public void drawLine(int x1, int y1, int x2, int y2) {
        writer.element("line", "class", "lineStyle");
        writer.attribute("x1", x1);
        writer.attribute("y1", y1);
        writer.attribute("x2", x2);
        writer.attribute("y2", y2);
        writer.end();
    }

    public void drawBox(int x, int y, int width, int height) {
        writer.element("rect", "class", "boxStyle");
        writer.attribute("x", x);
        writer.attribute("y", y);
        writer.attribute("width", width);
        writer.attribute("height", height);
        writer.end();
    }

    public void drawText(int x, int y, NodeContent content) {
        writer.element("text");
        writer.attribute("font-size", fontSize);
        writer.attribute("y", y);
        for (int i = 0; i < content.getNumberOfLines(); i++) {
            String line = content.getLine(i);
            String style = content.getStyle(i);
            drawText(x, line, style);
        }
        writer.end();
    }

    public void drawText(int x, int y, List<String> lines) {
        writer.element("text");
        writer.attribute("font-size", fontSize);
        writer.attribute("y", y);
        for (String line : lines) {
            drawText(x, line, "");
        }
        writer.end();
    }

    private void drawText(int x, String line, String style) {
        if (line != null) {
            writer.element("tspan", "text-anchor", "middle", "style", style);
            writer.attribute("x", x);
            writer.attribute("dy", textHeight);
            writer.cdata(line);
            writer.end();
        }
    }
}
